<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="代号" prop="number">
        <el-input v-model="dataForm.number" placeholder="代号"/>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="登录帐号"/>
      </el-form-item>
      <el-form-item :class="{ 'is-required': !dataForm.id }" label="密码" prop="password">
        <el-input v-model="dataForm.password" type="password" placeholder="密码"/>
      </el-form-item>
      <el-form-item :class="{ 'is-required': !dataForm.id }" label="确认密码" prop="comfirmPassword">
        <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码"/>
      </el-form-item>
      <!-- <el-form-item label="邮箱"
                    prop="email">
        <el-input v-model="dataForm.email"
                  placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="手机号"
                    prop="mobile">
        <el-input v-model="dataForm.mobile"
                  placeholder="手机号"></el-input>
      </el-form-item>-->
      <el-form-item label="角色" prop="role">
        <el-select v-model="dataForm.role" placeholder="请选择">
          <el-option
            v-for="item in roleOptions"
            :key="item.roleId"
            :label="item.roleName"
            :value="item.roleId"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="状态"
                    size="mini"
                    prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>-->
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateUsers, addUsers } from '@/api/manage'
export default {
  data() {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.dataForm.password !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        number: '',
        userName: '',
        password: '',
        comfirmPassword: '',
        role: ''
      },
      students: [],
      dataRule: {
        number: [
          { required: true, message: '代号不能为空', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [{ validator: validatePassword, trigger: 'blur' }],
        comfirmPassword: [
          { validator: validateComfirmPassword, trigger: 'blur' }
        ],
        role: [{ required: true, message: '角色不能为空', trigger: 'blur' }]
        // email: [
        //   { required: true, message: '邮箱不能为空', trigger: 'blur' },
        //   { validator: validateEmail, trigger: 'blur' }
        // ],
        // mobile: [
        //   { required: true, message: '手机号不能为空', trigger: 'blur' },
        //   { validator: validateMobile, trigger: 'blur' }
        // ]
      },
      roleOptions: [
        {
          roleId: '1',
          roleName: '老师'
        },
        {
          roleId: '2',
          roleName: '学生'
        }
      ]
    }
  },
  methods: {
    init(row) {
      this.visible = true
      if (row) {
        this.dataForm.id = row._id
        this.dataForm.number = row.number
        this.dataForm.userName = row.username
        this.dataForm.password = row.password
        this.dataForm.comfirmPassword = row.password
        this.dataForm.role = row.role
      } else {
        this.dataForm.id = 0
        this.$nextTick(() => {
          this.$refs.dataForm.resetFields()
        })
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const data = {
            number: this.dataForm.number,
            username: this.dataForm.userName,
            password: this.dataForm.password,
            role: this.dataForm.role,
            userId: this.dataForm.id
          }
          if (this.dataForm.id) {
            updateUsers(data).then(res => {
              if (res.code === 1) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                const data = res.data
                this.$message.error(data.msg)
              }
            })
          } else {
            addUsers(data).then(res => {
              if (res.code === 1) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                const data = res.data
                this.$message.error(data.msg)
              }
            })
          }
        }
      })
    }
  }
}
</script>
